package com.itcast.mallapplet.controller.user;


import com.itcast.admin.dto.UserDTO;
import com.itcast.admin.entity.User;
import com.itcast.applet.dto.UserLoginDTO;
import com.itcast.applet.vo.UserLoginVO;
import com.itcast.constant.JwtClaimsConstant;
import com.itcast.context.BaseContext;
import com.itcast.mallapplet.service.UserService;
import com.itcast.properties.JwtProperties;
import com.itcast.result.Result;
import com.itcast.utils.AliOssUtil;
import com.itcast.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户端-user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信用户登录{}", userLoginDTO.getCode());
        //微信登录
        User user = userService.wxLogin(userLoginDTO);

        //为微信用户生成Jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());

        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .avatar(user.getAvatar())
                .name(user.getName())
                .build();

        return Result.success(userLoginVO);
    }


    @GetMapping("/queryAll")
    @ApiOperation("查询全部用户信息")
    public Result<List<User>> userQueryAll() {
        log.info("查询全部用户信息");
        List<User> userList = userService.userQueryAll();
        return Result.success(userList);
    }


    //接口已通过-OK
    @PostMapping("/amend")
    @ApiOperation("修改用户")
    public Result amendUser(UserDTO userDTO, MultipartFile image) {
        log.info("修改用户的信息为:{}", userDTO);
        log.info("用户修改头像上传,{}", image);
        if(image != null) {
            try {
                //原始文件名
                String originalFilename = image.getOriginalFilename();
                //截取原始文件名的后缀 37-57cj.png
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//截取最后一个点的后半部分
                //构建新文件名称
                String objectName = UUID.randomUUID().toString() + extension;
                //文件的请求路径
                String upload = aliOssUtil.upload(image.getBytes(), objectName);
                userDTO.setAvatar(upload);
            } catch (IOException e) {
                log.error("文件上传失败：{}", e);
            }
        }
        userService.amendUser(userDTO);
        return Result.success();
    }


    //查找当前用户信息
    @GetMapping("/query")
    @ApiOperation("查询当前用户信息")
    public Result<User> userQuery() {
        log.info("查询当前用户信息");
        User user = userService.userQuery();
        return Result.success(user);
    }


}
